package com.alan.shiro.config;

import com.alan.shiro.bean.TestUser;
import com.alan.shiro.utils.PropertyUtil;
import freemarker.ext.beans.BeansWrapper;
import freemarker.template.TemplateHashModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.HashMap;

@Configuration
public class FreemarkerConfig {

    @Autowired
    FreeMarkerViewResolver freeMarkerViewResolver;

    /**
     * 配置通过 staticClass.properties 文件来定义前端调用的java类
     * @return
     * @throws IOException
     */
    @Bean
    public FreemarkerStaticModels freemarkerStaticModels() {
        FreemarkerStaticModels freemarkerStaticModels = FreemarkerStaticModels.getInstance();
        freemarkerStaticModels.setStaticModels(PropertyUtil.loadProps("staticClass.properties") );
        return freemarkerStaticModels;
    }

    /**
     * PostConstruct 在构造函数之后执行，init（）方法之前执行。PreDestroy（）方法在destroy（）方法知性之后执行
     */
    @PostConstruct
    public void setStaticModels() {
        FreemarkerStaticModels staticModels = FreemarkerStaticModels.getInstance();
        //设置在模板中调用的名称，和要调用的Java类
        staticModels.setStaticModels("TestUser", TestUser.class.getName());
        freeMarkerViewResolver.setAttributesMap(staticModels);
        //配置通过 staticClass.properties 文件来定义前端调用的java类
        freeMarkerViewResolver.setAttributesMap(freemarkerStaticModels());
    }

}
